package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.ConfigManager;
import cn.net.greenet.common.policy_service.PolicySupport;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Webcategory;
import cn.net.greenet.db.bean.WebcategoryId;
import cn.net.greenet.generalfile.logic.DataChange;
import cn.net.greenet.generalfile.logic.GeneralFileProcess;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.util.List;

/**
 * Created by 师梁 on 2015/7/30.
 */
@Service("GetWeb2DBService")
public class GetWeb2DBService {
    public boolean get(String path,String MessageType,String policy_instance,GeneralFileProcess pro){

        boolean result = false;
        if (path != null) {
            List<String> list = pro.getContent(path, 3);
            if (!list.isEmpty()) {
                SessionFactory sf = HibernateUtil.getSessionFactory();
                Session session = sf.openSession();
                session.clear();
                Transaction tx = session.beginTransaction();
                try {
                    Webcategory web = null;
                    WebcategoryId webid = null;
                    for (String line : list) {
                        web = new Webcategory();
                        webid = new WebcategoryId();
                        String[] ss = line.split("\t");
                        String WebTypeID = ss[1];
                        int id = DataChange.hex2dec(WebTypeID);
                        ss[1] = id + "";
                        webid.setHostName(ss[0]);
                        webid.setWebTypeId(id);
                        web.setId(webid);
                        web.setWebTypeName(ss[2]);
                        session.save(web);
                    }
                    tx.commit();
                    result = true;
                } catch (Exception e) {
                    tx.rollback();
                    result = false;
                } finally {
                    session.close();
                }
                try {
                    PolicySupport policySupport = PolicySupport.getInstance();
                    String policy = policySupport.getApi().getMessage("0xC8", "默认库", 0).getSvnFile().getContent();
                    String comment = policySupport.getApi().getMessage("0xC8", "默认库", 0).getSvnFile().getComment();
                    policySupport.getApi().updatePolicy("0xC8", "默认库", 0, policy, comment);
                } catch (Exception e) {
                }

            }
        }
        return result;
    }
}
